﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using CHR.Contract.Common;
using Kingdee.BOS;
using Kingdee.BOS.App;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Serialization;

namespace CHR.App.Core.Common
{
    /// <summary>
    /// 通用服务
    /// </summary>
    public class CommonService : ICommonService
    {
        /// <summary>
        /// 获取业务对象发布的自定义参数
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="formKey">业务对象key</param>
        /// <returns></returns>
        public Dictionary<string, object> GetCustomParams(Context ctx, string formKey)
        {
            const string strSql = @"SELECT FCUSTOMPARAMS FROM T_META_CONSOLEDETAIL WHERE FOBJECTID=@formId";
            var param = new[] { new SqlParam("@formId", DbType.String, formKey) };
            string pars = DBUtils.ExecuteScalar(ctx, strSql, string.Empty, param);
            var obj = Kingdee.BOS.JSON.KDObjectConverter.DeserializeObject<Dictionary<string, object>>(pars);
            return obj;
        }

        public object GetSystemProfile(Context ctx, long orgId, string category, string key, object defvalue)
        {
            var xml = GetXml(ctx, orgId, category);
            if (xml == null || xml.ToString().Trim().Length == 0)
            {
                xml = GetXml(ctx, 0, category);
                if (xml == null || xml.ToString().Trim().Length == 0)
                {
                    return defvalue;
                }
            }

            IMetaDataService service = ServiceHelper.GetService<IMetaDataService>();
            FormMetadata meta = (FormMetadata)service.Load(ctx, category, false);
            BusinessInfo info = meta.BusinessInfo;
            DcxmlBinder binder = new DynamicObjectDcxmlBinder(info);
            DcxmlSerializer target = new DcxmlSerializer(binder);
            //切换到中性语言，获取主差量
            CultureInfo inv = new CultureInfo(2052); //中性语言
            binder.Culture = inv;
            DynamicObject obj = (DynamicObject)target.DeserializeFromString(xml.ToString());

            //如果参数不为空
            if (!string.IsNullOrEmpty(key))
            {
                return obj[key];
            }
            return defvalue;
        }

        private object GetXml(Context ctx, long orgId, string category)
        {
            string sql = "select fparameters from t_bas_sysparameter where (forgid=@orgId) and fparameterobjid=@objId";

            SqlParam[] paramList = new SqlParam[2];
            paramList[0] = new SqlParam("@orgId", DbType.Int64, orgId);
            paramList[1] = new SqlParam("@objId", DbType.String, category);

            //object xml = DBUtils.ExecuteScalar(ctx, sql, param);
            object xml = DBUtils.ExecuteScalar<Object>(ctx, sql, null, paramList);
            return xml;
        }

        /// <summary>
        /// 清业务数据缓存
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="formId">需要清理的单据标识,如:HR_PA_PayPlan</param>
        /// <param name="id">需要清除的单据主键id,如:主键为1的薪酬方案数据</param>
        public void ClearCache(Context ctx, string formId, Int64 id)
        {
            IMetaDataService metaDateService =Kingdee.BOS.App.ServiceHelper.GetService<IMetaDataService>();
            FormMetadata meta = metaDateService.Load(ctx, formId, false) as FormMetadata;
            IBusinessDataService bussinessDateService = Kingdee.BOS.App.ServiceHelper.GetService<IBusinessDataService>();
            bussinessDateService.ClearCache(ctx, meta.BusinessInfo.GetDynamicObjectType(), new List<object>()
            {
                id
            });
        }

        /// <summary>
        /// 清业务数据缓存(批量)
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="formId">需要清理的单据标识,如:HR_PA_PayPlan</param>
        /// <param name="id">需要清除的单据主键id,如:主键为1的薪酬方案数据</param>
        public void ClearCache(Context ctx, string formId, List<Int64> idList)
        {
            IMetaDataService metaDateService = Kingdee.BOS.App.ServiceHelper.GetService<IMetaDataService>();
            FormMetadata meta = metaDateService.Load(ctx, formId, false) as FormMetadata;
            IBusinessDataService bussinessDateService = Kingdee.BOS.App.ServiceHelper.GetService<IBusinessDataService>();
            bussinessDateService.ClearCache(ctx, meta.BusinessInfo.GetDynamicObjectType(), idList.ConvertAll<Object>(p => p));
        }

    }
}
